package com.example.dao.impl;

import com.example.dao.BuyChanceDao;
import com.example.entity.customerAdmin.BuyChance;
import com.example.utils.BaseDao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class BuyChanceDaoImpl implements BuyChanceDao {
    @Override
    public int add (BuyChance buyChance) {
        int i=-1;
        try {
            Object[] objects=new Object[6];
            objects[0]=buyChance.getBcTitle ();
            objects[1]=buyChance.getSaleMoney ();
            objects[2]=buyChance.getSignDate ();
            objects[3]=buyChance.getSaleStage ();
            objects[4]=buyChance.getDealProbability ();
            objects[5]=buyChance.getBcSource ();
            String sql="insert into buy_chance set(bc_title,sale_money,sign_date,sale_stage,dealprobability,bc_source) value(?,?,?,?,?,?)";
            i = BaseDao.modify (sql, objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public int del (int id) {
        int i=-1;
        Object[] objects=new Object[1];
        objects[0]=id;
        try {
            i=BaseDao.modify ("delete from buy_chance where id=?",objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public List<BuyChance> selectAll (int page,int limit) {
        List<BuyChance> buyChanceList=new ArrayList<> ();
        Object[] objects=new Object[2];
        objects[0]=page;
        objects[1]=limit;
        ResultSet select = BaseDao.select ("select * from buy_chance limit ?,?", objects);
        try {
            while (select.next ()){
                BuyChance buyChance=new BuyChance ();
                buyChance.setId (select.getInt ("id"));
                buyChance.setBcTitle (select.getString ("bc_title"));
                buyChance.setSaleMoney (select.getString ("sale_money"));
                buyChance.setSignDate (select.getString ("sign_date"));
                buyChance.setSaleStage (select.getString ("sale_stage"));
                buyChance.setDealProbability (select.getString ("dealprobability"));
                buyChance.setBcSource (select.getString ("bc_source"));
                buyChanceList.add (buyChance);
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return buyChanceList;
    }

    @Override
    public int getCount () {
        int i=-1;
        ResultSet select = BaseDao.select ("select count(1) getCount from buy_chance ", null);
        try {
            while (select.next ()){
                i=select.getInt ("getCount");
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public BuyChance selectId (int id) {
        BuyChance buyChance=new BuyChance ();
        Object[] objects=new Object[1];
        objects[0]=id;
        ResultSet select = BaseDao.select ("select * from buy_chance where id=?", objects);
        try {
            while (select.next ()){
                buyChance.setId (select.getInt ("id"));
                buyChance.setBcTitle (select.getString ("bc_title"));
                buyChance.setSaleMoney (select.getString ("sale_money"));
                buyChance.setSignDate (select.getString ("sign_date"));
                buyChance.setSaleStage (select.getString ("sale_stage"));
                buyChance.setDealProbability (select.getString ("dealprobability"));
                buyChance.setBcSource (select.getString ("bc_source"));

            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return buyChance;
    }
}
